package com.enterprise.projectManager.service.implemention;

import com.enterprise.projectManager.model.Contract;
import com.enterprise.projectManager.rope.ContractRope;
import com.enterprise.projectManager.service.ContractService;
import jakarta.transaction.Transactional;
import lombok.RequiredArgsConstructor;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Optional;

@Service
@RequiredArgsConstructor
@Transactional
public class ContractImpl implements ContractService {
    private final ContractRope contractRope;
    private final CountServiceImpl countService;

    @Override
    public Contract createcontra(Contract contract) {
        if (contract.getContractId() == null) contract.setContractId(countService.generalContractId());
        return contractRope.save(contract);
    }

    @Override
    public Boolean deletecontra(String id1) {
        Optional<Contract> optionalContract = contractRope.findById(id1);
        contractRope.delete(optionalContract.get());
        return Boolean.TRUE;    //待进一步判断
    }

    @Override
    public Contract updatecontra(Contract contract) {
        return contractRope.save(contract);
    }


    @Override
    public List<Contract> listcontra(int limitcon) {
        // 从数据库中检索前 limit 个项目
        return contractRope.findAll(PageRequest.of(0, limitcon)).getContent();
    }

    @Override
    public List<Contract> listAll() {
        return contractRope.findAll();
    }

    @Override
    public List<String> findAllDistinctFirstCompany() {
        return contractRope.findAllDistinctFirstCompany();
    }

    @Override
    public List<String> findAllDistinctSecondCompany() {
        return contractRope.findAllDistinctSecondCompany();
    }




    @Override
    public List<Contract> filterContract(Contract contract) {
        System.out.println(contract);
        return contractRope.filterContract(
                contract.getContractId(),
                contract.getContractName(),
                contract.getContractNature(),
                contract.getContractState()
                );
    }

}
